<?php

namespace app\api\controller\zi;

use app\api\model\OrderM;
use app\common\controller\Api;
use think\Validate;

class OrderList extends Api
{
    protected $noNeedRight = 'index';

    public function index()
    {
        $validate = new Validate();
        if (!$validate->check(input(), [
            'page'   => 'require|integer',
            'search' => 'min:1'
        ])) {
            $this->error($validate->getError());
        }
        $page   = input('page');
        $search = input('search');
        $user   = $this->auth->getUser();
        $query  = OrderM::where([
            'user_id'  => $user['id'],
            'payed_is' => OrderM::PAYED_IS_YES,
        ]);
        if ($search) {
            $query->where('sn', 'like', $search);
        }
        $list = $query->order('id desc')->paginate(10, ['page' => $page])->each(function (&$item) {
            $item['info'] = json_decode($item['info'], true);
        });
        $this->success('', [
            'lei_ji_ding_dan' => OrderM::where(['user_id' => $user['id'], 'payed_is' => OrderM::PAYED_IS_YES])->count(),
            'zong_xiao_fei'   => OrderM::where(['user_id' => $user['id'], 'payed_is' => OrderM::PAYED_IS_YES])->sum('total_fee'),
            'list'            => $list,
        ]);
    }
}
